package com.wjp.dinosaur.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.wjp.dinosaur.dao.mapper.UserMapper;
import com.wjp.dinosaur.dao.pojo.User;
import com.wjp.dinosaur.service.LoginService;
import com.wjp.dinosaur.util.Constant;
import com.wjp.dinosaur.util.JWTUtils;
import com.wjp.dinosaur.vo.ErrorCode;
import com.wjp.dinosaur.vo.RegisterVo;
import com.wjp.dinosaur.vo.Result;
import com.wjp.dinosaur.vo.parm.LoginParms;
import com.wjp.dinosaur.vo.parm.RegisterParams;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.concurrent.TimeUnit;


@Service
public class LoginImpl implements LoginService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private HttpServletRequest httpServletRequest;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Override
    public Result login(LoginParms loginParms) {
        Long account = loginParms.getAccount();
        String password = loginParms.getPassword();
        if(!(password != null || account != 0)){
            return Result.fail(ErrorCode.ACCOUNT_PWD_NOT_EXIST.getCode(), ErrorCode.ACCOUNT_PWD_NOT_EXIST.getMsg());
        }
        String password2 = DigestUtils.md5Hex(password + Constant.MD5);
        User user = userMapper.selectById(account);
        System.out.println(account);
        System.out.println(user);
        if (user != null && user.getPassword().equals(password2)){
            String token = JWTUtils.createToken(account);
            redisTemplate.opsForValue().set("TOKEN_" + token, JSON.toJSONString(user), 1000, TimeUnit.DAYS);
            return Result.success(token);
        }
        return Result.fail(ErrorCode.ACCOUNT_PWD_NOT_EXIST.getCode(),ErrorCode.ACCOUNT_PWD_NOT_EXIST.getMsg());
    }

    @Override
    public Result loginout() {
        String token = httpServletRequest.getHeader("token");
        redisTemplate.delete("TOKEN_"+token);
        return null;
    }

    @Override
    public Result register(RegisterParams registerParams) {
        String password = registerParams.getPassword();
        String userName = registerParams.getUserName();

        if (StringUtils.isBlank(password)||
        StringUtils.isBlank(userName)){
            return Result.fail(ErrorCode.PARAMS_ERROR.getCode(), ErrorCode.PARAMS_ERROR.getMsg());
        }
        registerParams.setPassword(DigestUtils.md5Hex(password + Constant.MD5));
        User user = new User();
        BeanUtils.copyProperties(registerParams,user);
        try{
            userMapper.insert(user);
        }catch (Exception e){
            return Result.fail(ErrorCode.EXISTING_PHONE.getCode(), ErrorCode.EXISTING_PHONE.getMsg());
        }
        // token
        String token = JWTUtils.createToken(user.getAccount());
        redisTemplate.opsForValue().set("TOKEN_" + token, JSON.toJSONString(user), 1000, TimeUnit.DAYS);
        RegisterVo registerVo = new RegisterVo();
        registerVo.setAccount(user.getAccount());
        registerVo.setToken(token);
        return Result.success(registerVo);
    }

    @Override
    public User checkToken(String token) {
        if (StringUtils.isBlank(token)){
            return null;
        }
        Map<String, Object> stringObjectMap = JWTUtils.checkToken(token);
        if (stringObjectMap == null){
            return null;
        }
        String userJson = redisTemplate.opsForValue().get("TOKEN_" + token);
        if (StringUtils.isBlank(userJson)){
            return null;
        }
        return JSON.parseObject(userJson, User.class);
    }
}
